package org.zfes.snowier.cloud.zbss.cms.content.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.content.service.ICmsContentTagService;
import org.zfes.snowier.cloud.zbss.cms.tag.model.CmsTag;
import org.zfes.snowier.cloud.zbss.cms.tag.service.ICmsTagService;
import org.zfes.snowier.cloud.zbss.cms.content.dao.CmsContentTagMapper;
import org.zfes.snowier.cloud.zbss.cms.content.model.CmsContentTag;
import java.util.stream.Stream;
@Service
public class CmsContentTagServiceImpl implements ICmsContentTagService{
 	@Autowired
	private CmsContentTagMapper cmsContentTagMapper;
 	@Autowired
 	private ICmsTagService cmsTagService;
	
 	//===============增
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsContentTag(CmsContentTag cmsContentTag) {
		ZBeanUtil.validateBean(cmsContentTag);
		cmsContentTag.setStatus(Byte.valueOf("1"));
		cmsContentTag.setCreateTime(ZDateUtil.newDate());
		cmsContentTag.setUpdateTime(ZDateUtil.newDate());
		cmsContentTagMapper.insertSelective(cmsContentTag);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void createCmsContentTag(Long contentId, String tagNames, Long siteId) {
		ZAssert.notNull(contentId, "请选择要操作的contentId");
		ZAssert.notNull(siteId, "请选择要操作的siteId");
		ZAssert.notNull(tagNames, "请选择标签");
		ZAssert.hasText(tagNames, "请填写标签");
		
		//取出tagName列表
		List<String> tagNameList=Arrays.asList(tagNames.split(","));
		
		//获取tagId
		for(int i=0;i<tagNameList.size();i++) {
		Optional<CmsTag> tagOp = cmsTagService.loadCmsTagListByTagName(tagNameList.get(i), siteId);
			if(tagOp.isPresent()) {//该标签已存在
				//引用次数加1
				cmsTagService.makeRefCountTotal(tagNameList.get(i), siteId);
				
				//向cms_content_tag添加内容标签记录
				CmsContentTag contentTag = new CmsContentTag();
				ZBeanUtil.setCreateInfo(contentTag);
				contentTag.setContentId(contentId);
				contentTag.setTagId(tagOp.get().getId());
			
				cmsContentTagMapper.insertSelective(contentTag);
			}
			else {//该标签不存在
				//添加标签
				CmsTag tag = new CmsTag();
				ZBeanUtil.setCreateInfo(tag);
				tag.setSiteId(siteId);
				tag.setTagName(tagNameList.get(i));
				cmsTagService.createCmsTag(tag);
			
				//向cms_content_tag添加内容记录
				CmsContentTag contentTag = new CmsContentTag();
				ZBeanUtil.setCreateInfo(contentTag);
				contentTag.setContentId(contentId);
				contentTag.setTagId(cmsTagService.loadCmsTagListByTagName(tagNameList.get(i), siteId).get().getId());
				
				cmsContentTagMapper.insertSelective(contentTag);
			}
		}
	}

	//=============删

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsContentTag(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsContentTagMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsContentTagByContentId(Long contentId) {
		ZAssert.notNull(contentId, "请选择要操作的contentId");
		
		//获取tagIds
		List<CmsContentTag> contentTags = cmsContentTagMapper.selectByContentId(contentId);
		if(!contentTags.isEmpty()) {//该内容有标签时
			//删除content_tag表中记录
			cmsContentTagMapper.deleteByContentId(contentId);
			String tagIds="";
			for(int i=0;i<contentTags.size();i++) {
				tagIds=tagIds+contentTags.get(i).getTagId();
			}
			cmsTagService.subtractRefCountTotal(tagIds);
		}
		
	}
	//=============改
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsContentTag(CmsContentTag cmsContentTag) {
		ZBeanUtil.validateBean(cmsContentTag);
		cmsContentTag.setUpdateTime(ZDateUtil.newDate());
		cmsContentTagMapper.updateByPrimaryKeySelective(cmsContentTag);
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<Long> selectContentIdListByTagId(Long tagId) {
		List<Long> contentIdList=new ArrayList<Long>();	
		contentIdList=cmsContentTagMapper.selectContentIdListByTagId(tagId);
		return contentIdList;
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsContentTagStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsContentTagMapper.updateStatus( id,  status) ;
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsContentTagStatusByContentId(Long contentId, Byte status) {
		ZAssert.notNull(contentId, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsContentTagMapper.updateStatusByContentId(contentId, status);
	}
	//============查
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsContentTag> loadCmsContentTagById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsContentTagMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsContentTagDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsContentTagMapper.selectListCount(pm), cmsContentTagMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<CmsContentTag> loadCmsContentTagByContentId(Long contentId) {
		if(contentId!=null){
			return cmsContentTagMapper.selectByContentId(contentId);
		}
		return new ArrayList<CmsContentTag>();
	}

	
}